/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elektra;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.Chat;

/**
 *
 * @author adee
 */
public class Elektra {
    private Mongo mongoDB;
    private DB messages;
    private static final Logger LOG = Logger.getLogger(Elektra.class.getName());
    private static final String UNDEFINED_RESPOND = "Nem vágom mire gondolsz";
    private CommandProcessor commandProcessor;
    private Chat chat;
    private EmotionEngine elektraEmotion;
    private MessageNormaliser messageNormaliser;
    
    public Elektra(Chat chat) {
        try {
            this.messageNormaliser = new MessageNormaliser();
            this.elektraEmotion= new EmotionEngine();
            this.commandProcessor= new CommandProcessor(this);
            this.chat=chat;
            this.chat.addMessageListener(this.commandProcessor);
            mongoDB = new Mongo();
            LOG.info("Connected to MongoDB");
            messages = mongoDB.getDB("elektra");
            LOG.info("Message Database connected"); 
        } catch (UnknownHostException ex) {
            LOG.log(Level.SEVERE,"Nem sikerült csatlakozni az adatbázishoz",ex);
        } catch (MongoException ex) {
            LOG.log(Level.SEVERE,"Mongo para",ex);
        }

    }
    
    
    
    private void setMessageType(BasicDBObject object){
        String type = "UNDEFINED";
        String message = object.get("message").toString().toLowerCase();
        if (message.contains("?")){
            type="QUESTION";
        }  
        
        if (message.contains("szia")){
            type="QREETING";
        }  
        
        
        object.append("type", type); 
    }
    
    private void saveMessage(String message){
       DBCollection collection = messages.getCollection("messages");
       BasicDBObject object = new BasicDBObject();
       object.append("message",message);
       setMessageType(object);
       
       
       collection.insert(object);
    }
    
    private String findRespond(String message){
         String result = UNDEFINED_RESPOND;
         String normalisedMessage = this.messageNormaliser.normaliseMessage(message);
         DBCollection messagesCollection = messages.getCollection("messages");
         DBCursor cursor = messagesCollection.find();
         while(cursor.hasNext()){
            DBObject smessage =  cursor.next();
            if (smessage.containsField("message")){
              if( smessage.get("message").toString().toLowerCase().equals(normalisedMessage)){
                result = smessage.get("respond").toString();
              }
            }     
         }
        
        if (result.equals(UNDEFINED_RESPOND)){
          saveMessage(normalisedMessage);
        }
        return result;
    }
    
    public String getRespond(String message){
        return this.elektraEmotion.mutateMessage(message,this.findRespond(message));
    }
    
    
    
}
